package com.zretc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.zretc.dao.UsersDao;
import com.zretc.entity.Users;
import com.zretc.impl.UsersDaoImpl;
import com.zretc.service.UsersService;
import com.zretc.serviceimpl.UsersServiceImpl;
import com.zretc.tools.MD5Util;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// UsersServlet要调用service所以这里需要创建
	// UsersService对象
	UsersService us = new UsersServiceImpl();
UsersDao ud=new UsersDaoImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1设置请求参数
		request.setCharacterEncoding("utf-8");

		// 2 获取op的值
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if ("".equals(op)) {
			// 获取前台传递过来的查询的关键字
			String keywords = "";
			if (request.getParameter("keywords") != null) {
				keywords = request.getParameter("keywords");
			}
			// 查询 将结果存储在属性范围内，转发到显示页面去

			List<Users> list = us.getUsers(keywords);
			
			request.setAttribute("list", list);
			
			//转发
			request.getRequestDispatcher("login.jsp").forward(request, response);

		} else if ("adduser".equals(op)) {
			// 3 获取用户名和密码
			String username = request.getParameter("username");
			String userpwd = request.getParameter("pwd");
			String userpwds = request.getParameter("pwds");
			String md5pwd = MD5Util.getEncodeByMd5(userpwd);
			String md5pwds = MD5Util.getEncodeByMd5(userpwds);
			System.out.println("username:" + username);
			System.out.println("userpwd:" + userpwd);
			Users u = new Users(username, md5pwd);
			// 4调用service等,明天接着写
			if (userpwd.equals(userpwds)) {
			boolean flag = us.addUsers(u);
			System.out.println("flag:"+flag);

			if (flag) {
					 JOptionPane.showMessageDialog(null, "注册成功"); 
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else {
				JOptionPane.showMessageDialog(null, "注册失败"); 
				request.getRequestDispatcher("register.jsp").forward(request, response);
			}
			}
			else{
				 JOptionPane.showMessageDialog(null, "两次密码不一致，请重新输入"); 
				request.getRequestDispatcher("register.jsp").forward(request, response);
			} {
				
			}
			// 5 根据结果跳转页面
//			if (flag) {
//				request.getRequestDispatcher("succ.jsp").forward(request, response);
//			}
//			else {
//				request.getRequestDispatcher("fail.jsp").forward(request, response);
//			}

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
